/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.playground.core.entity.boardgame;

/**
 * Represents a square in a board. A square can aways be refered by a position
 * in the board. A square can be occupied by a piece.
 * 
 * For example, a checkers game has 64 squares. The squares can be identified by
 * a line and a column. We can have a subclass called CheckersSquare with  the
 * methods getColumn and getLine. getColum would return position % 8, while 
 * getLine would return floor(position / 8).
 * 
 * In a game like monoply, the square's position is a simple integer, but each
 * square has its properties, which means that a subclass like "MonopolySquare"
 * should be created.
 * 
 * @author Tiago Peres
 */
public class Square {
    private int position;
    private Piece piece;
    
    public Square(int position){
        this.position = position;
    }

    public int getPosition() {
        return position;
    }

    /**
     * @return the piece occupying by this square or null if it's not occupied 
     * by any piece.
     */
    public Piece getPiece() {
        return piece;
    }

    public void setPiece(Piece piece) {
        this.piece = piece;
    }
    
    public boolean isFree(){
        return null == piece;
    }
    
    @Override
    public boolean equals(Object o){
        if (null != o && Square.class.isAssignableFrom(o.getClass())){
            if (((Square) o).getPosition() == this.getPosition()){
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 43 * hash + this.position;
        return hash;
    }
}
